package com.xfsg.core.log.config;

import com.aliyun.openservices.aliyun.log.producer.LogProducer;
import com.aliyun.openservices.aliyun.log.producer.ProducerConfig;
import com.xfsg.core.log.LogAppenderRegister;
import com.xfsg.core.log.listener.ConfigRefreshEventListener;
import com.xfsg.core.log.logger.AliyunLogger;
import com.xfsg.core.log.logger.PitayaLogger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

/**
 * @author ZhangYi
 * @Classname BladeLogAppenderAutoConfiguration
 * @Description 阿里云日志自动装配
 * @Date 2021/12/13 17:15
 */
@Configuration(proxyBeanMethods = false)
@Import(LogAppenderRegister.class)
@EnableConfigurationProperties({AliyunLogProperties.class})
@ConditionalOnProperty(prefix = "aliyun.log", name = "enabled", havingValue = "true")
@PropertySource(value = "classpath:/pitaya-aliyun-log.yml")
public class BladeAliyunLogAppenderAutoConfiguration {

	@Bean
	public PitayaLogger pitayaLogger() {
		return new PitayaLogger();
	}

	@Bean
	public AliyunLogItem aliyunLogItem(PitayaLogger pitayaLogger){
		AliyunLogItem aliyunLogItem = new AliyunLogItem();
		aliyunLogItem.setServiceId(pitayaLogger.getServiceId());
		return aliyunLogItem;
	}

	@Bean
	@ConditionalOnMissingBean
	public AliyunLogger aliyunLogger(LogProducer logProducer) {
		return new AliyunLogger(logProducer);
	}

	@Bean
	@ConditionalOnMissingBean
	public LogProducer logProducer(AliyunLogProperties properties) {
		ProducerConfig producerConfig = new ProducerConfig();
		AliyunLogProperties.AliyunProducerConfig producerProp = properties.getProducer();
		producerConfig.setTotalSizeInBytes(producerProp.getTotalSizeInBytes());
		producerConfig.setMaxBlockMs(producerProp.getMaxBlockMs());
		producerConfig.setIoThreadCount(producerProp.getIoThreadCount());
		producerConfig.setBatchSizeThresholdInBytes(producerProp.getBatchSizeThresholdInBytes());
		producerConfig.setBatchCountThreshold(producerProp.getBatchCountThreshold());
		producerConfig.setLingerMs(producerProp.getLingerMs());
		producerConfig.setRetries(producerProp.getRetries());
		producerConfig.setBaseRetryBackoffMs(producerProp.getBaseRetryBackoffMs());
		producerConfig.setMaxRetryBackoffMs(producerProp.getMaxRetryBackoffMs());
		return new LogProducer(producerConfig);
	}

	/**
	 * 如果有spring cloud和nacos 配置，动态刷新会导致log context 清空早期加入的内容
	 * @param properties
	 * @param logItem
	 * @return
	 */
	@Bean(name="customConfigRefreshEventListener")
	@ConditionalOnClass(name = "org.springframework.cloud.endpoint.event.RefreshEvent")
	public ConfigRefreshEventListener configRefreshEventListener(AliyunLogProperties properties, AliyunLogItem logItem){
		return new ConfigRefreshEventListener(properties, logItem);
	}

}
